﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using Microsoft.Expression.Interactivity.Layout;
using StrategyMap.BusinessLayer;

namespace StrategyMap.UserControls
{
    /// <summary>
    /// Node in strategy map - strategic target.
    /// </summary>
    /// <remarks></remarks>
    public partial class Node1 : UserControl
    {
        /// <summary>
        /// Occurs when [start drawing arrow event].
        /// </summary>
        /// <remarks></remarks>
        public event MouseButtonEventHandler StartDrawingArrowEvent;
        
        /// <summary>
        /// 
        /// </summary>
        public bool NodeTargetEntered;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Windows.FrameworkElement"/> class.
        /// </summary>
        /// <remarks></remarks>
        public Node1()
        {
            InitializeComponent();
            AddDragBehavior();
        }
        
        /// <summary>
        /// Gets the width of the n.
        /// </summary>
        /// <remarks></remarks>
        public double nWidth
        {
            get
            {
                return 160;
                //return Main.Width + 2 * Left.Width;
            }
            //set;
        }

        /// <summary>
        /// Gets the height of the n.
        /// </summary>
        /// <remarks></remarks>
        public double nHeight
        {
            get
            {
                return 80;
            }
        }

        /// <summary>
        /// Gets or sets the radius.
        /// </summary>
        /// <value>The radius.</value>
        /// <remarks></remarks>
        public int Radius
        {
            get
            {
                return (int)Main.RadiusX;
            }
            set
            {
                Main.RadiusX = value;
                Main.RadiusY = value;
            }
        }

        /// <summary>
        /// Adds the drag behavior.
        /// </summary>
        /// <remarks></remarks>
        private void AddDragBehavior()
        {
            MouseDragElementBehavior drag = new MouseDragElementBehavior();
            drag.ConstrainToParentBounds = true;

            Interaction.GetBehaviors(this).Add(drag);
        }

        /// <summary>
        /// Attachs the drag behavior.
        /// </summary>
        /// <remarks></remarks>
        public void AttachDragBehavior()
        {
            foreach (var behavior in Interaction.GetBehaviors(this))
            {
                if (behavior.GetType() == typeof(MouseDragElementBehavior))
                {
                    behavior.Attach(this);
                }
            }
        }

        /// <summary>
        /// Detachs the drag behavior.
        /// </summary>
        /// <remarks></remarks>
        private void DetachDragBehavior()
        {
            foreach (var behavior in Interaction.GetBehaviors(this))
            {
                if (behavior.GetType() == typeof(MouseDragElementBehavior))
                {
                    behavior.Detach();
                }
            }
        }

        /// <summary>
        /// Handles the MouseEnter event of the NodeLayoutRoot control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void NodeLayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            Left.Visibility = System.Windows.Visibility.Visible;
            Right.Visibility = System.Windows.Visibility.Visible;
            Upper.Visibility = System.Windows.Visibility.Visible;
            Lower.Visibility = System.Windows.Visibility.Visible;

            Main.StrokeThickness = 3;
            this.NodeLayoutRoot.Cursor = Cursors.Hand;
        }

        /// <summary>
        /// Handles the MouseLeave event of the NodeLayoutRoot control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void NodeLayoutRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            HideRectagles();

            Main.StrokeThickness = 1;
            this.NodeLayoutRoot.Cursor = Cursors.Arrow;
        }

        /// <summary>
        /// Handles the Clicked event of the NodeLayoutRoot_MouseLeave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void NodeLayoutRoot_MouseLeave_Clicked(object sender, MouseEventArgs e)
        {
            HideRectagles();
            this.NodeLayoutRoot.Cursor = Cursors.Arrow;
        }

        /// <summary>
        /// Changes the mouse leave_ click.
        /// </summary>
        /// <remarks></remarks>
        public void ChangeMouseLeave_Click()
        {
            this.NodeLayoutRoot.MouseLeave -= NodeLayoutRoot_MouseLeave;
            this.NodeLayoutRoot.MouseLeave += NodeLayoutRoot_MouseLeave_Clicked;
        }

        /// <summary>
        /// Changes the mouse leave_ un click.
        /// </summary>
        /// <remarks></remarks>
        public void ChangeMouseLeave_UnClick()
        {
            Main.StrokeThickness = 1;
            this.NodeLayoutRoot.MouseLeave += NodeLayoutRoot_MouseLeave;
            this.NodeLayoutRoot.MouseLeave -= NodeLayoutRoot_MouseLeave_Clicked;
        }

        /// <summary>
        /// Hides the rectagles.
        /// </summary>
        /// <remarks></remarks>
        public void HideRectagles()
        {
            Left.Visibility = System.Windows.Visibility.Collapsed;
            Right.Visibility = System.Windows.Visibility.Collapsed;
            Upper.Visibility = System.Windows.Visibility.Collapsed;
            Lower.Visibility = System.Windows.Visibility.Collapsed;

        }

        /// <summary>
        /// Nodes the layout root_ mouse leave_ cancel stroke.
        /// </summary>
        /// <remarks></remarks>
        public void NodeLayoutRoot_MouseLeave_CancelStroke()
        {
            Main.StrokeThickness = 1;
        }

        /// <summary>
        /// Handles the MouseEnter event of the ConnectionPoint control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void ConnectionPoint_MouseEnter(object sender, MouseEventArgs e)
        {
            ((Rectangle)sender).Fill = new SolidColorBrush(Colors.Green);
        }

        /// <summary>
        /// Handles the MouseLeave event of the ConnectionPoint control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void ConnectionPoint_MouseLeave(object sender, MouseEventArgs e)
        {
            ((Rectangle)sender).Fill = new SolidColorBrush(ColorTranslator.TranslateFromHex("#FF89E7F3"));
        }

        /// <summary>
        /// Handles the MouseLeftButtonDown event of the ConnectionPoint control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void ConnectionPoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ((Rectangle)sender).Fill = new SolidColorBrush(Colors.Blue);
            //Interaction.GetBehaviors(this).RemoveAt(0);
            DetachDragBehavior();
            
            if (StartDrawingArrowEvent != null)
            {
                StartDrawingArrowEvent(this, e);
            }
        }

        /// <summary>
        /// Handles the MouseLeftButtonUp event of the ConnectionPoint control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void ConnectionPoint_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            
        }

        ////public event MouseButtonEventHandler StopDrawingArrowEvent;
        /// <summary>
        /// Handles the MouseLeftButtonUp event of the NodeLayoutRoot control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void NodeLayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
        }

        /// <summary>
        /// Handles the MouseLeftButtonDown event of the UserControl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        }
                
        /// <summary>
        /// Handles the MouseLeftButtonDown event of the NodeLayoutRoot control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void NodeLayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        }
    }    
}
